#!/usr/bin/env coffee

import main from './ipc/main.coffee'
import USER from './user.coffee'
import {show as auto_update_show} from './ipc/auto_update.coffee'
import {join} from 'path'
import {png} from '@/config/dir.coffee'
import os from 'os'
import {shell}  from "electron"
import {HTTP} from '@/config/site.coffee'

platform = os.platform()

{Menu,Tray,app,globalShortcut} = require('electron')

# 外面声明防止被垃圾回收没了
trayIco = new Tray join(png, "trayTemplate.png")
menu = [
  {
    label: '媒体库'
    click: =>
      shell.openExternal HTTP+'video_library.html?user_token='+USER.ID
      return
  }
  {
    label: '检测更新'
    click: auto_update_show
  }
  {
    type: 'separator'
  }
]

MENU = menuDefault = Menu.buildFromTemplate([
  {
    label: '显示界面'
    click: main
  }
  ...menu
  {
    label: '退出'
    click: =>
      #app.quiting = true
      app.quit()
  }
])

menuRec = Menu.buildFromTemplate menu

click = [
  'click'
  =>
    main()
    return
]

trayIco.on(
  'right-click'
  (e)=>
    trayIco.popUpContextMenu(MENU)
    return
)

export rec = =>
  trayIco.off ...click
  MENU = menuRec
  return

export default =>
  trayIco.on ...click
  MENU = menuDefault
  return
